/**
 * 
 */
package info.niwota.fwrite;

import android.app.Notification;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * @author qiangli
 * 
 */
public class Preferences {
	private static final String STATUSBAR_TOGGLE = "settings.statusbar.toggle";
	private static final String EDIT_AUTOSPACE_TOGGLE = "settings.editing.autospace.toggle";
	private static final String EDIT_LINESIZE = "settings.editing.linesize";
	public static final int DEFAULT_LINESIZE = 48;
	private static final String SESSION_PEER = "session.peer";

	/**
	 * 
	 */
	private Preferences() {
	}

	public static boolean enableStatusbar(Context ctx) {
		final boolean defvalue = true;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(STATUSBAR_TOGGLE, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}

	public static boolean enableAutoSpace(Context ctx) {
		final boolean defvalue = true;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(EDIT_AUTOSPACE_TOGGLE, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}

	public static int getEditLinesize(Context context) {
		int defval = DEFAULT_LINESIZE;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(context);
			String s = pref.getString(EDIT_LINESIZE, null);
			if (s == null) {
				return defval;
			}
			int v = Integer.parseInt(s);
			return v;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defval;
	}

	public static void putPeer(Context ctx, String peer) {
		final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ctx);
		pref.edit().putString(SESSION_PEER, peer).commit();
	}
	
	public static String getPeer(Context ctx, String def) {
		try {
			final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ctx);
			return pref.getString(SESSION_PEER, def);
		} catch (Exception e) {
		}
		return def;
	}

	public static int getNotifications(Context ctx) {
		int note = 0;
		final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ctx);
		if (pref.getBoolean("settings.notification.vibrate.toggle", false)) {
			note |= Notification.DEFAULT_VIBRATE;
		}
		if (pref.getBoolean("settings.notification.flash.toggle", false)) {
			note |= Notification.DEFAULT_LIGHTS;
		}
		if (pref.getBoolean("settings.notification.sound.toggle", false)) {
			note |= Notification.DEFAULT_SOUND;
		}
		return note;
	}
}
